package com.wl.mq;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;

/**
 * Represents a connection with a queue
 * Created by zhen on 2016/10/11.
 */
public abstract class EndPoint {

    protected Channel channel;

    protected Connection connection;

    protected String endPointName;

    public EndPoint(String endpointName) throws IOException {

        this.endPointName = endpointName;
        //Create a connection factory
        ConnectionFactory factory = new ConnectionFactory();

        //hostname,username,password of your rabbitmq server
        factory.setHost("114.215.159.182");

        factory.setUsername("admin");

        factory.setPassword("admin");

        //getting a connection
        connection = factory.newConnection();

        //creating a channel
        channel = connection.createChannel();

        //declaring a queue for this channel. If queue does not exist,
        //it will be created on the server.
        channel.queueDeclare(endpointName,false,false,false,null);
    }
    /**
     * 关闭channel和connection。并非必须，因为隐含是自动调用的。
     * @throws IOException
     */
    public void close() throws IOException{
        this.channel.close();
        this.connection.close();
    }
}
